package com.ruoyi.web.controller.aggregation;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.aggregation.dto.PageDTO;
import com.ruoyi.system.domain.aggregation.Product;
import com.ruoyi.system.service.aggregation.IProductService;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@RestController
@RequestMapping("/agg/product")
public class ProductController
{
    @Autowired
    private IProductService productService;

    /**
     * 商品列表
     */
    @PostMapping("/page")
    public AjaxResult page(@RequestBody PageDTO pageDTO)
    {
        try
        {
            PageHelper.startPage(pageDTO.getPageIndex().intValue(), pageDTO.getPageSize().intValue());
            Product query = new Product();
            List<Product> list = productService.selectProductList(query);
            PageInfo<Product> pageInfo = new PageInfo<>(list);
            return AjaxResult.success(pageInfo);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 添加商品
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Product product)
    {
        try
        {
            productService.insertProduct(product);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 编辑商品
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Product product)
    {
        try
        {
            productService.updateProduct(product);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 商品详情
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam Long id)
    {
        try
        {
            Product product = productService.selectProductById(id);
            return AjaxResult.success(product);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * app扫码查看商品列表
     */
    @GetMapping("/scanCode")
    public AjaxResult scanCode(@RequestParam Long merchantId)
    {
        try
        {
            List<Product> list = productService.selectProductByMerchantId(merchantId);
            return AjaxResult.success(list);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}

